package com.example.tiantian.contact;

import com.example.tiantian.R;

import android.os.Parcel;
import android.os.Parcelable;

public class ContactInfo implements Parcelable {
    public long _id;
    public String name;
    public String mobilephone;
    public String officephone;
    public String familyphone;
    public String address;
    public String othercontact;
    public String email;
    public String position; // 职位
    public String company;
    public String zipcode;
    public String remark;
    public int imageid = R.drawable.image1;// 给每个联系人设置一个默认的头像
    public byte[] photo;
    public int length;

    public String getName() {
        return name == null ? getPhone() : name;
    }

    public String getPhone() {
        if (mobilephone != null && mobilephone.length() > 0) {
            return mobilephone;
        }
        if (familyphone != null && familyphone.length() > 0) {
            return familyphone;
        }
        if (officephone != null && officephone.length() > 0) {
            return officephone;
        }
        return othercontact;
    }

    public ContactInfo() {
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeLong(_id);
        out.writeString(name);
        out.writeString(mobilephone);
        out.writeString(officephone);
        out.writeString(familyphone);
        out.writeString(address);
        out.writeString(othercontact);
        out.writeString(email);
        out.writeString(position);
        out.writeString(company);
        out.writeString(zipcode);
        out.writeString(remark);
        out.writeInt(imageid);
        out.writeInt(length);
        if (photo == null) {
            photo = new byte[length];
        }
        out.writeByteArray(photo);
    }

    public static final Parcelable.Creator<ContactInfo> CREATOR = new Parcelable.Creator<ContactInfo>() {
        public ContactInfo createFromParcel(Parcel in) {
            return new ContactInfo(in);
        }

        public ContactInfo[] newArray(int size) {
            return new ContactInfo[size];
        }
    };

    private ContactInfo(Parcel in) {
        _id = in.readLong();
        name = in.readString();
        mobilephone = in.readString();
        officephone = in.readString();
        familyphone = in.readString();
        address = in.readString();
        othercontact = in.readString();
        email = in.readString();
        position = in.readString();
        company = in.readString();
        zipcode = in.readString();
        remark = in.readString();
        imageid = in.readInt();
        length = in.readInt();
        photo = new byte[length];
        in.readByteArray(photo);

    }

    public boolean findInfo(String str) {
        return name.contains(str) || mobilephone.contains(str) || officephone.contains(str)
                || familyphone.contains(str);
    }

    public void setInfo(ContactInfo info) {
        name = info.name;
        mobilephone = info.mobilephone;
        officephone = info.officephone;
        familyphone = info.familyphone;
        address = info.address;
        othercontact = info.othercontact;
        email = info.email;
        position = info.position;
        company = info.company;
        zipcode = info.zipcode;
        remark = info.remark;
        imageid = info.imageid;
        photo = info.photo;
        length = info.length;
    }
}
